/* 
 * Contains all Javascript coding for Kukui Kup Billboard Display 
 * All response functions for each supported Visualization
 * (Table, Column Chart, BioHeatMap) are implemented in this file.
 * Any additional visualizations can be added by loading the appropriate
 * library, creating a response handler function, and adding the type 
 * to the global array, validTypes, in div-classes.js.
 */

// Necessary imports to draw Google Visualizations Table and Column Charts.
google.load('visualization', '1', {packages:['table']});
google.load('visualization', '1', {packages:['columnchart']});

/* 
 * Handles the query response for all Table visualizations.
 *
 * @param response Is the query response from Google Spreadsheet containing data.
 * @param divID Is the specified div ID to draw the table.
 */
function tableResponseHandler(response, divID) {
  // If there was an error querying the spreadsheet, notify the user and end the query.
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }
  
  // Get the response table from the query.
  var data = response.getDataTable();
  
  // Since the table's default font is in a standard 12pt, need to apply
  // font size 20 to make it viewable from a distance.
  var numColumns = data.getNumberOfColumns();
  var numRows = data.getNumberOfRows();
  // Each cell in the table needs the property 'font-size' applied.
  for (var i = 0; i < numRows; i++) {
    for (var j = 0; j < numColumns; j++) {
      data.setProperties(i, j, {style: 'font-size: 20px;'});
    }
  }
  
  // Finally, draw the table in the specified div tag.
  var table = new google.visualization.Table(document.getElementById(divID));
  // Allow HTML for the style property to be used.
  table.draw(data, {allowHtml: true});
  
}

/* 
 * Handles the query response for all Column Chart visualizations.
 *
 * @param response Is the query response from Google Spreadsheet containing data.
 * @param divID Is the specified div ID to draw the table.
 * @param optionsParam Is an associative array holding options specific to Column Charts.
 */
function colChartResponseHandler(response, divID, optionsParam) {
	
  // If there was an error querying the spreadsheet, notify the user and end the query.
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }
  
  // Draw the visualization in the specified div tag.
  var data = response.getDataTable();
  var table = new google.visualization.ColumnChart(document.getElementById(divID));
  table.draw(data, optionsParam);
  
}

/* 
 * Handles the query response for all BioHeatMap visualizations. 
 *
 * @param response Is the query response from Google Spreadsheet containing data.
 * @param divID Is the specified div ID to draw the table.
 * @param optionsParam Is an associative array holding options specific to BioHeatMaps.
 */
function bhmResponseHandler(response, divID, optionsParam) {

  // If there was an error querying the spreadsheet, notify the user and end the query.
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }
  
  // Draw the visualization in the specified div tag.
  var data = response.getDataTable();
  heatmap = new org.systemsbiology.visualization.BioHeatMap(document.getElementById(divID));
  heatmap.draw(data, optionsParam);
}

/* 
 * Writes the current date on the document by using
 * two arrays that hold the Month and Day of the week,
 * the day number is grabbed from the Date object.
 * e.x. Monday, October 10, 2010
 */
function printDateNow() {
  var now = new Date();
  var weekday = new Array(7);
  weekday[0]="Sunday";
  weekday[1]="Monday";
  weekday[2]="Tuesday";
  weekday[3]="Wednesday";
  weekday[4]="Thursday";
  weekday[5]="Friday";
  weekday[6]="Saturday";
  
  var month = new Array(12);
  month[0]="January";
  month[1]="February";
  month[2]="March";
  month[3]="April";
  month[4]="May";
  month[5]="June";
  month[6]="July";
  month[7]="August";
  month[8]="September";
  month[9]="October";
  month[10]="November";
  month[11]="December";
  
  // This function is called directly where the date is to be
  // printed, therefore just use the document.write method.
  document.write(weekday[now.getDay()] + ", " + month[now.getMonth()] 
                 + " " + now.getDate() + ", " + now.getFullYear());
  // Update the time every hour.
  setTimeout("printDateNow()", 3600000);
}